/* ********************************* */
/* imagesender.c                      */
/* ras7					*/
/* ********************************* */

#include "eyebot.h"
#include <stdlib.h>

#define MIN(a,b) (a<b?a:b)
#define MAX(a,b) (a>b?a:b)

float wht_mult_red=1;
float wht_mult_green=1;
float wht_mult_blue=1; 

int main()
{ int key;  colimage img;
  CAMInit(NORMAL);
  // init connection
  if(OSInitRS232(SER115200, NONE, SERIAL1) != 0){
    LCDPrintf("init rs323 error");
    return 1;
  }
  LCDSetPrintf(3,1,"initialize cam\n");
  int a=0;
for (a=0; a<50; a++)
	{
	CAMGetColFrame(&img, FALSE);
	}
  
  LCDClear();
  LCDSetPrintf(2,1,"Take pictures\n");
  LCDMenu("SND","WHT","","END");
  key = KEYGet();
  while(key != KEY4){
	key = KEYGet();
  	if(key==KEY1){
		//CAMGetFrameRGB(&camera_buffer,0);
		send_image_data_serial();			
	}
	if(key==KEY2){
		CAMGetColFrame(&img, FALSE);
		set_white_point(img);	
	}
   }
  return 0;
}

int send_image_data_serial()
{
	int i,j;
	int count=0;
	LCDSetPrintf(4,1,"    SENDING    \n");
 
	// send rgb data
	int buffer_size = 176*144;
	//BYTE camera_buffer[buffer_size];
  typedef BYTE bigcolimage[144][176][3];
  bigcolimage big_img;
  BYTE big_img_send[144*176*3];
  CAMGetFrameRGB((BYTE *)big_img);
  LCDSetPrintf(5,1, " start sending \n");
for(j=0; j<144; j++)
	{
	for(i=0; i<176; i++)
		{
  		int cr=wht_mult_red*(big_img[j][i][0])/10;
  		int cg=wht_mult_green*(big_img[j][i][1])/10;
  		int cb=wht_mult_blue*(big_img[j][i][2])/10;
  		big_img[j][i][0]=big_img[j][i][1]=big_img[j][i][2]=255;

  		if(cr<255) big_img[j][i][0]=cr; //cr;
  		if(cg<255) big_img[j][i][1]=cg; //cg;
  		if(cb<255) big_img[j][i][2]=cb; //cb;

		//correct_colourB(*big_img[j][i][1],*big_img[j][i][2],*big_img[j][i][3]);
		if(OSSendCharRS232(big_img[j][i][2], SERIAL1)!=0) return 1;
		if(OSSendCharRS232(big_img[j][i][1], SERIAL1)!=0) return 1;
		if(OSSendCharRS232(big_img[j][i][0], SERIAL1)!=0) return 1;		
		//LCDSetPrintf(6,1, " %d %d \n",i,j);
		}	
	}

	LCDSetPrintf(4,1,"    TRANSFER    ");
	LCDSetPrintf(5,1,"    FINISHED    ");
	
OSWait(200);
LCDClear();
LCDSetPrintf(2,1,"Take pictures\n");
LCDMenu("SND","WHT","","END");
return 0;
}

void set_white_point(colimage img) {
    int row, column;
    int count = 0;
    int r=0,g=0,b=0;
    
    for (row = 1; row < 7; row ++)
        for (column = 165; column < 174; column ++)

  //  for (row = imagerows/2 - 2; row < (imagerows/2 + 3); row ++)
  //      for (column = imagecolumns/2 - 2; column < imagecolumns/2 + 3; column ++)
        {
            r+=img[row][column][0];
            g+=img[row][column][1];
            b+=img[row][column][2];
            count ++;
        }
    
        if (count != 0){
            r /= count;
            g /= count;
            b /= count;
    }
    
    int max = MAX(r, MAX(g,b));
    wht_mult_red=(((float)max)/r) * 10;
    wht_mult_green=(((float)max)/g) * 10;
    wht_mult_blue=(((float)max)/b) * 10;
    
    LCDClear();
    LCDSetPrintf(1,1,"new rgb values");
    LCDSetPos(2,0);
    LCDPrintf(" r:%3.3f\n", (float)wht_mult_red / 10);
    LCDPrintf(" g:%3.3f\n", (float)wht_mult_green / 10);
    LCDPrintf(" b:%3.3f\n", (float)wht_mult_blue / 10);
OSWait(200);
LCDClear();
LCDPrintf("Take pictures\n");
LCDMenu("SND","WHT","","END");
}
/*
void set_white_point(colimage img)
{
  int row, column;
  int count = 0;
  int r=0,g=0,b=0;

  for (row = imagerows/2 - 2; row < (imagerows/2 + 3); row ++)
    for (column = imagecolumns/2 - 2; column < imagecolumns/2 + 3; column ++)
    {
      r+=img[row][column][0];
      g+=img[row][column][1];
      b+=img[row][column][2];      
      count ++;
    }

  if (count != 0){
    r /= count;
    g /= count;
    b /= count;
  }
  int max = MAX(r, MAX(g,b));

  wht_mult_red=((float)max)/r*10;
  wht_mult_green=((float)max)/g*10;
  wht_mult_blue=((float)max)/b*10;

  LCDClear();
  LCDMenu("", "", "", "OK");
  LCDSetPos(2,0);
  LCDPrintf("r: %d\n", r);
  LCDSetPos(3,0);
  LCDPrintf("g: %d\n", g);
  LCDSetPos(4,0);
  LCDPrintf("b: %d\n", b);
LCDSetPos(5,0);
LCDPrintf("mg: %f\n",wht_mult_green);
LCDSetPos(6,0);
LCDPrintf("mb: %f\n",wht_mult_blue);


  KEYWait(KEY4); 
  LCDClear();
  LCDPrintf("Take pictures\n");
  LCDMenu("SND","WHT","","END");
}
*/

